package cn.iocoder.yudao.hires.service.notice.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.hires.entity.NoticeReviewDO;
import cn.iocoder.yudao.hires.mapper.NoticeApplicationMapper;
import cn.iocoder.yudao.hires.mapper.NoticeReviewMapper;
import cn.iocoder.yudao.hires.service.notice.NoticeReviewService;
import cn.iocoder.yudao.hires.vo.PostTionShortVo;
import cn.iocoder.yudao.hires.vo.noticereview.NoticeReviewPageReqVO;
import cn.iocoder.yudao.hires.vo.noticereview.NoticeReviewSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.NOT_EXISTS;

/**
 * 下游-面试评价 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class NoticeReviewServiceImpl implements NoticeReviewService {

    @Resource
    private NoticeReviewMapper noticeReviewMapper;
    @Resource
    private NoticeApplicationMapper noticeApplicationMapper;

    @Override
    public Long createNoticeReview(NoticeReviewSaveReqVO createReqVO) {
        // 插入
        NoticeReviewDO noticeReview = BeanUtils.toBean(createReqVO, NoticeReviewDO.class);
        PostTionShortVo category = noticeApplicationMapper.getCategory(noticeReview.getPositionId());
        noticeReview.setPositionCategoryId(category.getCategoryId());
        noticeReview.setPositionCategoryName(category.getCategoryName());
        noticeReviewMapper.insert(noticeReview);
        // 返回
        return noticeReview.getId();
    }

    @Override
    public void updateNoticeReview(NoticeReviewSaveReqVO updateReqVO) {
        // 校验存在
        validateNoticeReviewExists(updateReqVO.getId());
        // 更新
        NoticeReviewDO updateObj = BeanUtils.toBean(updateReqVO, NoticeReviewDO.class);
        noticeReviewMapper.updateById(updateObj);
    }

    @Override
    public void deleteNoticeReview(Long id) {
        // 校验存在
        validateNoticeReviewExists(id);
        // 删除
        noticeReviewMapper.deleteById(id);
    }

    private void validateNoticeReviewExists(Long id) {
        if (noticeReviewMapper.selectById(id) == null) {
            throw exception(NOT_EXISTS);
        }
    }

    @Override
    public NoticeReviewDO getNoticeReview(Long id) {
        return noticeReviewMapper.selectById(id);
    }

    @Override
    public PageResult<NoticeReviewDO> getNoticeReviewPage(NoticeReviewPageReqVO pageReqVO) {
        return noticeReviewMapper.selectPage(pageReqVO);
    }

    @Override
    public List<NoticeReviewDO> getNoticeReviewList(Long tenantId) {
        return noticeReviewMapper.selectList(new LambdaQueryWrapper<NoticeReviewDO>().eq(NoticeReviewDO::getTenantId, tenantId));
    }

}